package captivate.Veela_AS3
{
	import flash.display.*;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.MouseEvent;
	import flash.media.SoundTransform;

	[Event(name = 'MovieYukleniyor', type = 'flash.events.Event')]
	[Event(name = 'MovieYuklendi', type = 'flash.events.Event')]
	[Event(name = 'MovieBasliyor', type = 'flash.events.Event')]
	[Event(name = 'MovieBasladi', type = 'flash.events.Event')]
	[Event(name = 'SlideBasliyor', type = 'flash.events.Event')]
	[Event(name = 'SlideBasladi', type = 'flash.events.Event')]
	[Event(name = 'SlideBitiyor', type = 'flash.events.Event')]
	[Event(name = 'SlideBitti', type = 'flash.events.Event')]
	[Event(name = 'FrameGiriyor', type = 'flash.events.Event')]
	[Event(name = 'FrameGirdi', type = 'flash.events.Event')]
	[Event(name = 'FrameCikiyor', type = 'flash.events.Event')]
	[Event(name = 'FrameCikti', type = 'flash.events.Event')]
	[Event(name = 'MovieBitiyor', type = 'flash.events.Event')]
	[Event(name = 'MovieBitti', type = 'flash.events.Event')]
	[Event(name = 'MovieDuruyor', type = 'flash.events.Event')]
	[Event(name = 'MovieDurdu', type = 'flash.events.Event')]
	[Event(name = 'MovieTekrarOynuyor', type = 'flash.events.Event')]
	[Event(name = 'MovieTekrarOynadi', type = 'flash.events.Event')]
	[Event(name = 'MovieKapatiliyor', type = 'flash.events.Event')]
	[Event(name = 'MovieKapatildi', type = 'flash.events.Event')]
	public class capProxy extends EventDispatcher
	{
		
		import com.STS.ELearning.Captivate.events.QuestionWidgetEvent;
		import com.STS.ELearning.Classes.CaptivateLoader;
		import com.STS.ELearning.VO.VideoVO;
		import com.adobe.captivate.events.CPQuestionEventData;
		import com.adobe.captivate.events.CPQuestionSubmitEvent;
		
		import mx.controls.SWFLoader;
		import mx.events.ResizeEvent;
		
		public static const MovieYukleniyor:String="MovieYukleniyor";
		public static const MovieYuklendi:String="MovieYuklendi";
		public static const MovieBasliyor:String="MovieBasliyor";
		public static const MovieBasladi:String="MovieBasladi";
		public static const SlideBasliyor:String="SlideBasliyor";
		public static const SlideBasladi:String="SlideBasladi";
		public static const SlideTiklaniyor:String="SlideTiklaniyor";
		public static const SlideTiklandi:String="SlideTiklandi";
		
		
		
		public static const SlideBitiyor:String="SlideBitiyor";
		public static const SlideBitti:String="SlideBitti";
		public static const FrameGiriyor:String="FrameGiriyor";
		public static const FrameGirdi:String="FrameGirdi";
		public static const FrameCikiyor:String="FrameCikiyor";
		public static const FrameCikti:String="FrameCikti";
		public static const MovieBitiyor:String="MovieBitiyor";
		public static const MovieBitti:String="MovieBitti";
		public static const MovieDuruyor:String="MovieDuruyor";
		public static const MovieDurdu:String="MovieDurdu";
		public static const MovieTekrarOynuyor:String="MovieTekrarOynuyor";
		public static const MovieTekrarOynadi:String="MovieTekrarOynadi";
		public static const MovieKapatiliyor:String="MovieKapatiliyor";
		public static const MovieKapatildi:String="MovieKapatildi";
		
		import com.STS.ELearning.Classes.Helper;
		import com.STS.ELearning.Classes.IcerikYoneticisi;
		import com.STS.ELearning.Classes.KullaniciIslemleri;
		import com.STS.ELearning.Classes.MyAlert;
		import com.STS.ELearning.DAO.Kullanicilar;
		import com.STS.ELearning.VO.KonularVO;
		import com.STS.ELearning.VO.KullanicilarVO;
		import com.adobe.captivate.enums.CPQuestionScoringTypeEnum;
		import com.adobe.captivate.events.CPMoviePauseEvent;
		import com.adobe.captivate.events.CPMovieResumeEvent;
		import com.adobe.captivate.events.CPMovieStartEvent;
		import com.adobe.captivate.events.CPMovieStopEvent;
		import com.adobe.captivate.events.CPSlideEnterEvent;
		import com.adobe.captivate.events.CPSlideExitEvent;
		import com.adobe.captivate.events.CaptivateEvent;
		import com.adobe.captivate.widgets.*;
		
		import flash.utils.describeType;
		
		import mx.charts.CategoryAxis;
		import mx.controls.Alert;
		import mx.core.Application;
		import mx.core.SoundAsset;
		import mx.core.Window;
		import mx.effects.SoundEffect;
		import mx.events.CloseEvent;
		import mx.events.FlexEvent;
		import mx.events.FlexNativeWindowBoundsEvent;
		import mx.events.SliderEvent;
		import mx.managers.SystemManager;
		
		
		protected var cap:*;
		protected var mov:*;
		protected var dis:IEventDispatcher;
		protected var activeSld:*=null;
		protected var sinavClick:*;
		protected var ipucuBtn:*;
		protected var ipucuImg:*;
		protected var hataImg:*;
		protected var hataKare:*;
		protected var fps:int=30;
		protected var totalFrmCount:int=0;
		protected var slideCount:int=0;
		protected var activeSlide:int=0;
		public var result:Boolean=false;
		public var movieBitti:Boolean=false;
		public var cancelable:Boolean=true;
		public var width:int=1024;
		public var height:int=576;
		public var kullanici:KullanicilarVO;
		public var aktifVideo:VideoVO;
		
		public var capVariables:Object=new Object();
		public function capProxy(av:VideoVO)
		{
			aktifVideo=av;
			kullanici=IcerikYoneticisi.getInstance().Kullanici;
			capVariables["KullaniciAdi"]=kullanici.ad;
			capVariables["Soyad"]=kullanici.soyad;
			capVariables["Email"]=kullanici.email;
			
		}
		
		
		public function get soundTransform():SoundTransform
		{
			return cap.soundTransform;
		}
		public function set soundTransform(v:SoundTransform):void
		{
			cap.soundTransform=v;
		}
		public function load(captivateMainTimeline:*):void
		{
			cap=captivateMainTimeline;
			this.width=cap.width;
			this.height=cap.height;
			trace(cap.cpMovieXForTOC);			
			try{cap.PlaybarProperties.hasExit[0].setChildren(0);}catch(err:Error){}
			try{cap.PlaybarProperties.hasMute[0].setChildren(0);}catch(err:Error){}
			dispatchEvent(new Event(MovieYukleniyor));
			DegiskenDegerleriniAta(cap);
			mov=cap.movie;
			dis=(cap.eventDispatcher as EventDispatcher);
			
			
			fps=cap.rdinfoFPS;
			totalFrmCount=cap.rdinfoFrameCount;
			slideCount=cap.rdinfoSlideCount;
			/*txtTopSlide.text="Slayt Sayısı:"+slideCount;
			var ts=Helper.getSure((totalFrmCount/30)*1000);
			txtToplam.text="Toplam Süre: "+ts.saat+"sa "+ts.dkk+"dk "+ts.snn+"sn";*/
			dis.addEventListener(CPMovieStartEvent.CPMOVIESTARTEVENT,cpMovieStartf,false,100);
			dis.addEventListener(Event.ENTER_FRAME,cpEnterFramef,false,-1);
			dis.addEventListener(CPSlideEnterEvent.CPSLIDEENTEREVENT,cpSlideBasladi,false,100);
			dis.addEventListener(CPSlideExitEvent.CPSLIDEEXITEVENT,cpSlideBitti,false,100);				
			dis.addEventListener(CPMoviePauseEvent.CPMOVIEPAUSEEVENT,cpMovieDurdu,false,100);
			dis.addEventListener(CPMovieResumeEvent.CPMOVIERESUMEEVENT,cpMovieTekrarOynadi,false,100);
			dis.addEventListener(CPMovieStopEvent.CPMOVIESTOPEVENT,cpMovieBitti,false,100);	
			dis.addEventListener(CPQuestionSubmitEvent.CPQUESTIONSUBMITEVENT,SoruCevaplandi,false,100);	
			dispatchEvent(new Event(MovieYuklendi));
		}
		
		public function UnLoad():Boolean
		{
			var evtk:Event=new Event(MovieKapatiliyor,false,true);
			dispatchEvent(evtk);
			if(!evtk.isDefaultPrevented())
			{
				
				temizle();
				evtk=new Event(MovieKapatildi,false,false);
				dispatchEvent(evtk);
				return true;
			}
			return false;
		}
		
		private function temizle():void
		{
			removeListeners();
			if(dis!=null)
			{
				dis.removeEventListener(CPMovieStartEvent.CPMOVIESTARTEVENT,cpMovieStartf);
				dis.removeEventListener(Event.ENTER_FRAME,cpEnterFramef);
				dis.removeEventListener(CPSlideEnterEvent.CPSLIDEENTEREVENT,cpSlideBasladi);
				dis.removeEventListener(CPSlideExitEvent.CPSLIDEEXITEVENT,cpSlideBitti);				
				dis.removeEventListener(CPMoviePauseEvent.CPMOVIEPAUSEEVENT,cpMovieDurdu);
				dis.removeEventListener(CPMovieResumeEvent.CPMOVIERESUMEEVENT,cpMovieTekrarOynadi);
				dis.removeEventListener(CPMovieStopEvent.CPMOVIESTOPEVENT,cpMovieBitti);
				
			}
			//cap.rdcmndExit=1;
			sinavClick=null;
			activeSld=null;
			ipucuImg=null;
			hataKare=null;
			ipucuBtn=null;
			mov=null;
			dis=null;
			if(cap!=null)
			{
				
				cap.rdcmndRewindAndStop=1;
				cap.stop();
				cap.rdClickHandler_mc.stop();
				/*
				for(var ix:int=0;ix<cap.rdClickHandler_mc["arr_Sprite"].length;ix++)
				{
					var xk:*=cap.rdClickHandler_mc.arr_Sprite[ix];
					for (var itk:int=0;itk<xk["listeners"].length;itk++)
					{
						xk["listeners"][itk]=null;
					}
					cap.rdClickHandler_mc["arr_Sprite"][ix]=null;
				}
				*/
				cap=null;
			}
			
		}
		private function removeListeners():void
		{
			if(activeSld!=null)
			{
				activeSld.removeEventListener(MouseEvent.CLICK,activeSldClick,true);
				activeSld.removeEventListener(MouseEvent.CLICK,activeSldClick,false);
			};
			if(ipucuImg!=null){
				ipucuImg.removeEventListener(MouseEvent.CLICK,ipucuImgClick,true);
				ipucuImg.removeEventListener(MouseEvent.CLICK,ipucuImgClick,false);
			}
			if(hataKare!=null){
				hataKare.removeEventListener(MouseEvent.CLICK,hataKareClick,true);
				hataKare.removeEventListener(MouseEvent.CLICK,hataKareClick,false);
			}
			if(ipucuBtn!=null){
				ipucuBtn.removeEventListener(MouseEvent.CLICK,ipucuBtnClick,true);
				ipucuBtn.removeEventListener(MouseEvent.CLICK,ipucuBtnClick,false);
			}
			if(sinavClick!=null)
			{
				sinavClick.removeEventListener(MouseEvent.CLICK,sinavClickf,true);
				sinavClick.removeEventListener(MouseEvent.CLICK,sinavClickf,false);
				
			}
		}
		
		public var hatagosterDenemeSayisi:int=2;
		public var _denemeSayisi:int=0;
		public function set denemeSayisi(val:int):void
		{
			_denemeSayisi=val;
		}
		public function get denemeSayisi():int
		{
			return _denemeSayisi;	
		}
		public var _toplamDenemeSayisi:int=0;
		public function set toplamDenemeSayisi(val:int):void
		{
			_toplamDenemeSayisi=val;
			
			if(cap!=null)
			{
				cap.DenemeSayisi=_toplamDenemeSayisi;
			}
		}
		public function get toplamDenemeSayisi():int
		{
			return _toplamDenemeSayisi;	
		}
		
		
		
		
		public function executeJavascipt(str:String):void
		{
			trace(str);
		}
		
		
		public function DegiskenDegerleriniAta(c:*):void
		{
			if(c!=null)
			{
				if(c["KullaniciAdi"])
				{
					c.KullaniciAdi=kullanici.ad;
				}
				if(c["HataGosterDenemeSayisi"])
				{
					hatagosterDenemeSayisi=c.HataGosterDenemeSayisi;
				}
			}
		}
		
		public function goster(o:*):void
		{
			if(o)
			{
				o.m_setVisibleCalledFromSlidelet = true;
				try{o.enable();}catch(error:Error){}
				try{o.showitem();}catch(error:Error){}			
			}
		}
		public function gizle(o:*):void
		{
			if(o)
			{
				try{o.disable();}catch(error:Error){}
				try{o.hideitem();}catch(error:Error){}
			}
		}
		
		public function cpMovieStartf(evt:Object):void{
			
			dispatchEvent(new Event(MovieBasliyor));
			this.movieBitti=false;
			denemeSayisi=0;
			toplamDenemeSayisi=0;
			dispatchEvent(new Event(MovieBasladi));
		}
		public function cpEnterFramef(evt:Object):void{
			if(cap==null)return;
			
			dispatchEvent(new Event(FrameGiriyor));
			if(cap.rdinfoCurrentFrame==0)
				dispatchEvent(new Event(MovieBasladi));
			
			/*var ts:Object=Helper.getSure(((totalFrmCount-cap.rdinfoCurrentFrame)/30)*1000);
			txtKalanSure.text="Kalan Süre: "+ts.saat+"sa "+ts.dkk+"dk "+ts.snn+"sn";
			*/
			dispatchEvent(new Event(FrameGirdi));
		}
		public function activeSldClick(mevt:MouseEvent):void{
			if(mevt.target!=mevt.currentTarget) return;
			dispatchEvent(new Event(SlideTiklaniyor));
			gizle(ipucuImg);
			if(denemeSayisi>=hatagosterDenemeSayisi)
			{
				goster(hataImg);
				goster(hataKare);
			}
			denemeSayisi=_denemeSayisi+1;
			dispatchEvent(new Event(SlideTiklandi));
		}
		
		public function hataKareClick(mevt:MouseEvent):void{
			gizle(ipucuImg);
			gizle(hataKare);
			gizle(hataImg);
			denemeSayisi=_denemeSayisi+1;
		}
		
		public function ipucuBtnClick(mevt:MouseEvent):void{
			goster(ipucuImg);
			denemeSayisi=_denemeSayisi+1;
			mevt.stopPropagation();
		}
		
		public function ipucuImgClick(mevt:MouseEvent):void{
			gizle(ipucuImg);
			mevt.stopPropagation();
		}
		
		public function sinavClickf(mevt:MouseEvent):void{
			mevt.stopPropagation();
		}
		public function SoruCevaplandi(mevt:Object)
		{
			var c:CPQuestionEventData=(mevt.questionEventData as CPQuestionEventData);
			if(c!=null)
			{
				var a:*;
				a=c.questionAnswered;
				a=c.interactionID;
				a=c.questionAnsweredCorrectly;
			}
			
		}
		public function cpSlideBasladi(evt:Object):void{
			
			mov=cap.movie;	
			dispatchEvent(new Event(SlideBasliyor));
			
			var sldType:String=evt.slideProperties.slideType;
			var ctarget:*=evt.currentTarget;
			var baslamaFrami:int=evt.frameNumber;
			var bitisFrami:int=evt.slideProperties.endFrame;
			var slideetiketi:String=evt.slideLabel;
			var target:*=evt.target;
		
			
			/*
				txtSimdikiSlide.text="Slayt No:"+evt.slideNumber;
			*/
			
			activeSlide=evt.slideNumber-1;
			
			
			
			denemeSayisi=0;
			
			activeSld=cap["Slide"+activeSlide+"_mc"];
			
			if(activeSld==null)
			{
				activeSld=cap["slide"+activeSlide+"__randomQuestion_mc"];
				if(activeSld!=null)
				{
					
					
					
					var prg:*=activeSld["m_qSlide_mc"];
					if(prg!=null)
					{
						var t:String=prg._progressMC.m_progressTextField.htmlText.replace("Question ","").replace(" of ",". Soru/ Toplam ");
						prg._progressMC.m_progressTextField.htmlText=t;
						
						if(true)
						{
							prg._question.answersDisabled = false;	
							prg._question.isTracked=false;
							prg._question._clearbutton.m_ItemXML[0].m_canResubmit.setChildren(1);
							prg._question.registerClearButton(prg._question._clearbutton);
							prg._question._submitbutton.m_ItemXML[0].m_canResubmit.setChildren(1);
							describeType(prg._question.registerSubmitButton);
							prg._question.registerSubmitButton(prg._question._submitbutton);
						}
					}
				}
			}
			if(activeSld==null)
			{
				activeSld=cap["slide"+activeSlide+"__question_mc"];
			}
			
			sinavClick=activeSld["ns"+(activeSlide+1)];
			if(!sinavClick)
			{
				sinavClick=activeSld["nswdbl"+(activeSlide+1)];
			}
			ipucuBtn=activeSld["nb"+(activeSlide+1)];
			ipucuImg=activeSld["ni"+(activeSlide+1)];
			hataImg=activeSld["nh"+(activeSlide+1)];
			hataKare=activeSld["nhk"+(activeSlide+1)];
			
			gizle(ipucuImg);
			gizle(hataKare);
			gizle(hataImg);
			
			if(activeSld!=null && sinavClick!=null)
			{
				activeSld.addEventListener(MouseEvent.CLICK,activeSldClick,false,100,false);
				sinavClick.addEventListener(MouseEvent.CLICK,sinavClickf,false,100,false);
				if((sinavClick.name as String).indexOf("nswdbl")>-1)
				{
					cap.doubleClickEnabled=true;
					sinavClick.doubleClickEnabled=true;
					sinavClick.addEventListener(MouseEvent.DOUBLE_CLICK,function(evtdbm:MouseEvent):void{
						var clickEv:MouseEvent=new MouseEvent(MouseEvent.CLICK,true,true,evtdbm.localX,evtdbm.localY,evtdbm.relatedObject,evtdbm.ctrlKey,evtdbm.altKey,evtdbm.shiftKey,evtdbm.buttonDown,evtdbm.delta,evtdbm.commandKey,evtdbm.controlKey,0);
						sinavClick.dispatchEvent(clickEv);
						evtdbm.preventDefault();
						cap.rdcmndGotoSlide=activeSlide+2;
					},false,100);
				}
			}
			if(hataKare!=null)
			{
				hataKare.addEventListener(MouseEvent.CLICK,hataKareClick,false,100,false);
			}
			if(ipucuBtn!=null)
			{
				ipucuBtn.addEventListener(MouseEvent.CLICK,ipucuBtnClick,false,100,false);
			}
			if(ipucuImg!=null)
			{
				ipucuImg.addEventListener(MouseEvent.CLICK,ipucuImgClick,false,100,false);
			}					
			dispatchEvent(new Event(SlideBasladi));
			if(activeSlide==0 && aktifVideo.sinav!=null && sldType!="Normal Slide")
			{
				cap.rdcmndGotoSlide=activeSlide+1;
			}
			
		}
		public function cpSlideBitti(evt:Object):void{
			dispatchEvent(new Event(SlideBitiyor));
			removeListeners();
			toplamDenemeSayisi+=denemeSayisi;
			denemeSayisi=0;
			dispatchEvent(new Event(SlideBitti));
		}
		public function cpMovieDurdu(evt:Object):void{
			
			dispatchEvent(new Event(MovieDuruyor));
			//trace(evt.userPause);
			this.movieBitti=false;
			dispatchEvent(new Event(MovieDurdu));
		}
		public function cpMovieTekrarOynadi(evt:Object):void{
			dispatchEvent(new Event(MovieTekrarOynuyor));
			//trace(evt.userPlay);
			this.movieBitti=false;
			dispatchEvent(new Event(MovieTekrarOynuyor));
		}
		public function cpMovieBitti(evt:Object):void{
			dispatchEvent(new Event(MovieBitiyor));
			removeListeners();
			this.movieBitti=true;
			denemeSayisi=0;
			toplamDenemeSayisi=0;
			dispatchEvent(new Event(MovieBitti));
		}
	}
	
	
}
